<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    img {
        vertical-align: top;
    }

    .boxl {
        position: relative;
        width: 350px;
        height: 350px;
        border: 1px solid #000000;
        margin: 100px;

    }

    .boxl>.max>span {
        display: none;
        position: absolute;
        top: 0;
        left: 0;
        width: 100px;
        height: 100px;
        background: rgba(255, 255, 0, .3);
        /* 鼠标样式 移动 */
        cursor: move;
    }

    .boxl>.boxb {
        display: none;
        position: absolute;
        top: 0;
        left: 360px;
        width: 400px;
        height: 400px;
        border: 1px solid #000000;
        overflow: hidden;
    }

    .boxl>.boxb>img {
        position: absolute;
        width: 1600px;
        height: 1600px;
    }
</style>

<body>
    <div class="boxl">
        <div class="max">
            <img src="001.jpg" alt="">
            <span></span>
        </div>

        <div class="boxb">
            <img src="0001.jpg" alt="">
        </div>
    </div>
</body>
<script>
    var spa = document.querySelector('span')
    var max = document.querySelector('.max')
    var boxb = document.querySelector('.boxb')
    var box = document.querySelector('.boxl')
    var boxbImg = document.querySelector('.boxb>img')

    max.onmouseover = function () {
        spa.style.display = 'block'
        boxb.style.display = 'block'
    }
    max.onmouseout = function () {
        spa.style.display = 'none'
        boxb.style.display = 'none'
    }

    //span遮罩层移动
    max.onmousemove = function (event) {
        event = event || window.event
        var x = event.clientX - this.offsetParent.offsetLeft - spa.offsetWidth / 2
        var y = event.clientY - this.offsetParent.offsetTop - spa.offsetHeight / 2

        if (x < 0) {
            x = 0
        } else if (x > box.offsetWidth - spa.offsetWidth) {
            x = box.offsetWidth - spa.offsetWidth
        }


        if (y < 0) {
            y = 0
        } else if (y > box.offsetHeight - spa.offsetHeight) {
            y = box.offsetHeight - spa.offsetHeight
        }
        spa.style.left = x + 'px'
        spa.style.top = y + 'px'


        boxbImg.style.left = - x * (boxbImg.offsetWidth / max.offsetWidth) + 'px'
        boxbImg.style.top = - y * (boxbImg.offsetHeight / max.offsetHeight) + 'px'
    }
</script>

</html>